﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace com.sddemo.uikit.controls
{
    public partial class AIOChart : UserControl
    {
        public AIOChart()
        {
            InitializeComponent();
        }

        #region  IOGraph

        public void InitCurve()
        {
            //**************************************
            ctlIOGraph.EarFlag = "Right";
            ctlIOGraph.ClearAxis();
            ctlIOGraph.ClearGraph();
            ctlIOGraph.CurveWidth = 1;
            ctlIOGraph.CurveColor = ColorTranslator.ToWin32(Color.Red);
            ctlIOGraph.CurveType = AChart.CurveTypeS.IO;
            ctlIOGraph.YTitle = "dBSPL";//"(dB HL)";
            ctlIOGraph.XTitle = "dBSPL";

            ctlIOGraph.Ynames = "160/150/140/130/120/110/100/90/80/70/60/50/40/30/20/10";
            ctlIOGraph.YMinValue = 10;
            ctlIOGraph.YMaxValue = 160;
            ctlIOGraph.YPointCount = 15;

            ctlIOGraph.Xnames = "10/20/30/40/50/60/70/80/90/100/110/120/130/140/150/160";
            ctlIOGraph.XMinValue = 10;
            ctlIOGraph.XMaxValue = 160;
            ctlIOGraph.XPointCount = 15;

            ctlIOGraph.BorderStyle = BorderStyle.None;

            ctlIOGraph.DrawUpAxis();

            ctlIOGraph.Paint += new System.Windows.Forms.PaintEventHandler(ctlIOGraph_Paint);
        }

        public void RemoveAllIOCurve()
        {
            ctlIOGraph.RemoveAllCurve();
        }
        public Color[] GetCurveColor()
        {
            var clrCurveColor = new Color[4];
            clrCurveColor[0] = pnl200Color.BackColor;
            clrCurveColor[1] = pnl500Color.BackColor;
            clrCurveColor[2] = pnl2000Color.BackColor;
            clrCurveColor[3] = pnl6000Color.BackColor;
            return clrCurveColor;
        }
        public void  SetCurveColor(Color[] clrCurveColor)
        {
            pnl200Color.BackColor= clrCurveColor[0];
            pnl500Color.BackColor= clrCurveColor[1];
            pnl2000Color.BackColor= clrCurveColor[2];
            pnl6000Color.BackColor= clrCurveColor[3];
        }

        public string[] GetCurveValue()
        {
            var clrCurveValue = new string[4];
            clrCurveValue[0] = lab200.Text;
            clrCurveValue[1] = lab500.Text;
            clrCurveValue[2] = lab2000.Text;
            clrCurveValue[3] = lab6000.Text;
            return clrCurveValue;
        }
        public void SetCurveValue(string[] clrCurveValue)
        {
           lab200.Text = clrCurveValue[0] ;
           lab500.Text = clrCurveValue[1] ;
           lab2000.Text = clrCurveValue[2];
           lab6000.Text = clrCurveValue[3];
        }
        public void SetCurveValue(int[] clrCurveValue)
        {
            lab200.Text = $"{clrCurveValue[0]}";
            lab500.Text = $"{clrCurveValue[1]}";
            lab2000.Text = $"{clrCurveValue[2]}";
            lab6000.Text = $"{clrCurveValue[3]}";
        }
        public void SetCurveValue(double[] clrCurveValue)
        {
            lab200.Text = $"{clrCurveValue[0]}";
            lab500.Text = $"{clrCurveValue[1]}";
            lab2000.Text = $"{clrCurveValue[2]}";
            lab6000.Text = $"{clrCurveValue[3]}";
        }
        public void AddIOCurve(double[] ArrInputList, double[] ArrDataList, Color CurvesColor)
        {
            ctlIOGraph.AddCurve();
            var CurvesIndex = ctlIOGraph.Curves.Count - 1;
            ctlIOGraph.Curves[CurvesIndex].CurveColor = CurvesColor;
            ctlIOGraph.Curves[CurvesIndex].CurveWith = 2;
            ctlIOGraph.Curves[CurvesIndex].PointCount = ArrInputList.Length;
            ctlIOGraph.Curves[CurvesIndex].CurveStyle = "Solid";
            for (int x = 0; x < ArrInputList.Length; x++)
            {
                ctlIOGraph.Curves[CurvesIndex].AddPoint((int)ArrInputList[x], ArrDataList[x]);
            }
        }
        /// <summary>
        /// Update IO Curve
        /// </summary>
        public void RefreshIOGraph()
        {
            double[] ArrInputList = getInputList();
            double[] ArrDataList200 = getInputValueList();
            double[] ArrDataList500 = getInputValueList();
            double[] ArrDataList2000 = getInputValueList();
            double[] ArrDataList6000 = getInputValueList();
            var ColorList = GetCurveColor();
            RemoveAllIOCurve();
            AddIOCurve(ArrInputList, ArrDataList200, ColorList[0]);
            AddIOCurve(ArrInputList, ArrDataList500, ColorList[1]);
            AddIOCurve(ArrInputList, ArrDataList2000, ColorList[2]);
            AddIOCurve(ArrInputList, ArrDataList6000, ColorList[3]);
            //AddCurve_IO(ArrInputList, ArrDataList200, ArrDataList500, ArrDataList2000, ArrDataList6000);
            RefreshChart();
        }
        public void RefreshChart()
        {
            //画线
            ctlIOGraph.DrawCurves();
            ctlIOGraph.Refresh();
        }

        private void ctlIOGraph_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Red, ButtonBorderStyle.Solid);
        }
        #endregion

        #region
        

        public double[] getInputList()
        {
            double[] SixthOctaves = new double[101];
            for (int i = 0; i < 101; i++)
            {
                SixthOctaves[i] = 20 + i;
            }
            return SixthOctaves;
        }
        public double[] getInputValueList(int basicData = 0)
        {
            Random rd = new Random();　　//无参即为使用系统时钟为种子
            basicData = rd.Next(0, 20);
            double[] SixthOctaves = new double[101];
            for (int i = 0; i < 101; i++)
            {
                //SixthOctaves[i] = rd.Next(0, 20) + i;
                SixthOctaves[i] = i + basicData;
                Debug.WriteLine($"{i}==>>>{SixthOctaves[i]}");
            }

            return SixthOctaves;
        }

       
        #endregion
    }
}
